// 管理员
<template>
  <div class="role">
    <div class="role-content">
      <div class="role-top">
        <el-input
          placeholder="请输入角色名称"
          v-model="paging.name"
          clearable
        ></el-input>
        <el-button type="primary" icon="el-icon-search" @click="Adminlist"
          >搜索</el-button
        >
        <el-button type="primary" icon="el-icon-edit" @click="addEquipment()"
          >添加</el-button
        >
      </div>
      <!--内容 -->
      <el-card class="box-card">
        <el-table :data="listData" style="width: 100%" border>
          <el-table-column prop="id" label="管理员ID" sortable>
          </el-table-column>
          <el-table-column prop="username" label="管理员名称">
          </el-table-column>
          <el-table-column prop="desc" label="管理员头像"> </el-table-column>
          <el-table-column label="管理员角色">
            <template slot-scope="scope">
                <!-- 采用过滤来判断id是否相等 -->
              <span v-for="(item,index) in scope.row.roleIds" :key="index">
                <el-tag>{{desclist.find((k) => k.id == item)? desclist.find((k) => k.id == item).name: item}}</el-tag>
              </span>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button size="mini" @click="handelEdit(scope.row)" class="blue"
                >编辑</el-button
              >
              <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
      </el-card>
      <!-- 添加编辑 -->
      <!-- 动态绑定值 -->
      <el-dialog
        :title="titleMap[dialogStatus]"
        :visible.sync="dialogFormVisible"
      >
        <el-form :model="ruleForm" ref="ruleForm" :rules="rules">
          <el-form-item
            label="登录名"
            prop="username"
            :label-width="formLabelWidth"
          >
            <el-input
              v-model="ruleForm.username"
              autocomplete="off"
              :disabled="state"
            ></el-input>
          </el-form-item>
          <el-form-item
            label="密码"
            prop="password"
            :label-width="formLabelWidth"
          >
            <el-input v-model="ruleForm.password" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="手机" prop="phone" :label-width="formLabelWidth">
            <el-input v-model="ruleForm.phone" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item
            label="真实姓名"
            prop="realname"
            :label-width="formLabelWidth"
          >
            <el-input v-model="ruleForm.realname" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="授权角色" :label-width="formLabelWidth">
            <template>
              <el-select
                v-model="ruleForm.roleIds"
                multiple
                placeholder="请选择"
                style="padding-left: 45px"
              >
                <el-option
                  v-for="item in desclist"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </template>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="affirm(ruleForm)">确 定</el-button>
        </div>
      </el-dialog>
      <!-- 分页 -->
      <div class="block">
        <!-- current-change当前页size-change每页条数current-page当前页数，支持 .sync 修饰符page-sizes每页显示条目个数，支持 .sync 修饰符-->
        <Paging @handle="handles" :allPages="paging"></Paging>
      </div>
    </div>
  </div>
</template>
<script>
// import Rolemanag from'../../api/rolemanage.js'
import Adminlist from "../../api/system/adminlist.js";
import Paging from "../../components/Paging"; //分页
export default {
  components: {
    Paging,
  },
  data() {
    return {
      state: false,//禁止按钮的状态
      desclist: [],//角色数据
      // 分页
      paging: {
        name: "",
        currentPage: 1, //当前的默认的页数
        eachpage: 10, //默认的页码
        total: 0, //初始值共数据条
        totalPageNo: 1,
      },
      listData: [], //初始化数据
      dialogFormVisible: false,
      FormVisible: false, //弹窗口
      select: [], //选中的权限'
      rules: [],
      data: [],
      authoriId: "",
      // 弹框状态值
      titleMap: {
        addEquipment: "新增",
        editEquipment: "编辑",
      },
      dialogStatus: "",
      //  输入框的宽度
      formLabelWidth: "120px",
      //  输入框的值
      ruleForm: {
        username: "",
        password: "",
        phone: "",
        realname: "",
        roleIds: "",
      },
    // 验证是否为空
      rules: {
        // 登入名
        username: [
          { required: true, message: "登录名不能为空!", trigger: "blur" },
        ],
        // 密码
        password: [
          { required: true, message: "密码不能为空!", trigger: "blur" },
        ],
        // 手机号
        phone: [{ required: true, message: "手机不能为空!", trigger: "blur" },
                                      //    验证手机号
           {required: true,pattern:/^1[3|4|5|6|7|8][0-9]{9}$/,message: '请输入正确的手机号',trigger: 'blur'}
        ],
        // 真实姓名
        realname: [
          { required: true, message: "真实姓名不能为空!", trigger: "blur" },
        ],
      },
    };
  },
// 创建后挂载
  created() {
    this.Adminlist();
    this.Role();
  },
  methods: {
    // 分页负值
    handles(val) {
      this.allPages = val;
      this.Adminlist();
    },
    //数据详情
    Adminlist() {
      let _this = this;
      Adminlist.adminlist(_this.paging).then((res) => {
        console.log(res);
        // 数据列表
        _this.listData = res.data.data.items;
        // 分页的值
        _this.paging.currentPage = res.data.data.pageNo;
        _this.paging.eachpage = res.data.data.pageSize;
        _this.paging.total = res.data.data.total;
        _this.paging.totalPageNo = res.data.data.totalPageNo;
      });
    },
    //角色初始化数据
    Role() {
      Adminlist.Rolemanage().then((res) => {
        console.log(res);
        this.desclist = res.data.data.items;
      });
    },
    // 删除
    handleDelete(row) {
      let _this = this;
      let id = row.id;
       // 删除提示框
      _this.$confirm("此操作将永久删除, 是否继续?", "提示", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        })
        .then(() => {
          Adminlist.deletes(id).then((res) => {
            _this.$message({
              message: "恭喜你，添加成功",
              type: "success",
            });
            _this.Adminlist();
          });
        })
        .catch(() => {
          _this.$information.error("已取消删除");
        });
    },
    // 添加编辑确认按钮
    affirm(ruleForm) {
        //  判断ruleForm值是否为空
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
            // 判断dialogStatus是否是addEquipment是就添加
          if (this.dialogStatus == "addEquipment") {
            console.log(this.ruleForm);
            Adminlist.addition(this.ruleForm).then((res) => {
              if (res.data.errmsg == "成功") {
                this.$message({
                  message: "恭喜你，添加成功",
                  type: "success",
                });
              } else {
                this.$message.error(res.data.errmsg);
              }
              this.Adminlist();
              this.dialogFormVisible = false;
            });
          }
            // 判断dialogStatus是否是editEquipment是就编辑
          if (this.dialogStatus == "editEquipment") {
            Adminlist.compile(this.ruleForm, this.compileId).then((res) => {
              if (res.data.errmsg == "成功") {
                this.$message({
                  message: "恭喜你，更新管理员成功",
                  type: "success",
                });
              } else {
                this.$message.error(res.data.errmsg);
              }
              this.Adminlist();
            });
            this.dialogFormVisible = false;
          }
        } else {
            // 为空提示
          this.$message.error("输入框不能为空！");
          return false;
        }
      });
    },
    // 添加
    addEquipment() {
      this.dialogFormVisible = true;
      this.dialogStatus = "addEquipment";//添加的值
      this.ruleForm = {};
      this.state = false;//禁止的状态
    },
    // 编辑
    handelEdit(row) {
      this.compileId = row.id;
      this.dialogFormVisible = true;
      this.dialogStatus = "editEquipment";//编辑的值
      this.ruleForm = row;
      this.state = true;//禁止的状态
    },
  },
  watch: {
    // 去除错误提示
    dialogFormVisible(val) {
      if (val == false) {
        this.$refs.ruleForm.clearValidate();
      }
    },
  },
};
</script>
<style scoped>
.role {
  width: 100%;
  line-height: 0px;
  overflow: hidden;
}
/* 添加编辑 */
.el-input {
  width: 300px;
}
.el-form/deep/.el-input__inner {
  width: 300px;
}
.el-form/deep/.el-form-item__content {
  width: 300px;
}
>>> .el-dialog/deep/.el-dialog__header {
  width: 50px;
}
.el-form-item/deep/.el-form-item__error {
  text-indent: 3rem;
}
/* 分页 */
.block {
  width: 40%;
  padding-top: 20px;
}
/* 字体居中 */
.blue {
  background: #409eff;
  color: #ffffff;
}
.el-table--enable-row-transition/deep/.el-table__body td {
  text-align: center;
}

.el-table/deep/th.is-sortable {
  cursor: pointer;
  text-align: center;
}
.el-table td,
.el-table/deep/th.is-leaf {
  text-align: center;
}
/* 布局 */
.el-card/deep/.box-card {
  height: 50%;
  overflow: hidden;
}
.role-content {
  width: 96%;
  margin: auto;
  overflow: hidden;
}
.el-input {
  width: 200px;
  margin-right: 10px;
}
.role-top {
  width: 100px;
  line-height: 60px;
}
.box-card {
  overflow: hidden;
}
</style>
